
load('summary_data/figA10.rda')

pal = c('blue','deepskyblue','darkslateblue','chocolate1','brown1','darkred')

inc6 = rbindlist(list(fig6_income, fig6_income_corr)) %>%
  mutate(class = factor(class, levels = unique(class))) %>%
  mutate(lty = if_else(str_starts(class, 'R'), 'solid', 'dashed'),
         a = if_else(str_starts(class, 'R'), 1, 1)) %>%
  ggplot(aes(
    x = pctile,
    y = sq,
    fill = class,
    group = class,
    color = class,
    linetype = lty,
    alpha = a
  )) +
  facet_wrap( ~ measure, ncol = 2) +
  geom_point(aes(alpha = a)) +
  geom_line() +
  theme_light() +
  scale_linetype_identity() +
  scale_alpha_identity() +
  scale_color_manual(
    values = pal[c(2:3, 5:6)],
    breaks = c(
      'Obama to Clinton',
      'Obama to Biden',
      'Romney to Trump \'16',
      'Romney to Trump \'20'
    )
  ) +
  scale_y_continuous(labels = scales::label_percent(),
                     limits = c(0, .45)) +
  labs(x = 'Measure Rank', y = '% Donor Retention', fill = element_blank()) +
  guides(
    alpha = 'none',
    fill = 'none',
    color = guide_legend(
      nrow = 1,
      byrow = TRUE,
      title = element_blank()
    )
  ) +
  theme(
    legend.position = 'bottom',
    axis.text.x = element_text(
      vjust = .85,
      hjust = 0.5,
      size = 7
    ),
    legend.text = element_text(size = 7),
    strip.background = element_rect(fill = 'white'),
    strip.text.x = element_text(
      face = 'bold',
      size = 10,
      color = 'black'
    ),
    legend.spacing.y = unit(0, 'cm'),
    text = element_text(face = 'bold')
  )

nw6 = rbindlist(list(fig6_nw, fig6_nw_corr)) %>%
  mutate(class = factor(class, levels = unique(class))) %>%
  mutate(lty = if_else(str_starts(class, 'R'), 'solid', 'dashed'),
         a = if_else(str_starts(class, 'R'), 1, 1)) %>%
  ggplot(aes(
    x = pctile,
    y = sq,
    fill = class,
    group = class,
    color = class,
    linetype = lty,
    alpha = a
  )) +
  facet_wrap( ~ measure, ncol = 2) +
  geom_point(aes(alpha = a)) +
  geom_line() +
  theme_light() +
  scale_x_continuous(breaks = seq(0, 100, 25)) +
  scale_linetype_identity() +
  scale_alpha_identity() +
  scale_color_manual(
    values = pal[c(2:3, 5:6)],
    breaks = c(
      'Obama to Clinton',
      'Obama to Biden',
      'Romney to Trump \'16',
      'Romney to Trump \'20'
    )
  ) +
  scale_y_continuous(labels = scales::label_percent(),
                     limits = c(0, .45)) +
  labs(x = 'Measure Rank', y = '% Donor Retention', fill = element_blank()) +
  guides(
    alpha = 'none',
    fill = 'none',
    color = guide_legend(
      nrow = 1,
      byrow = TRUE,
      title = element_blank()
    )
  ) +
  theme(
    legend.position = 'bottom',
    axis.text.x = element_text(
      vjust = .85,
      hjust = 0.5,
      size = 7
    ),
    legend.text = element_text(size = 7),
    strip.background = element_rect(fill = 'white'),
    strip.text.x = element_text(
      face = 'bold',
      size = 10,
      color = 'black'
    ),
    legend.spacing.y = unit(0, 'cm'),
    text = element_text(face = 'bold')
  ) +
  coord_cartesian(xlim = c(0, 99))

tmp = plot_grid(
  inc6 +
    theme(legend.position = 'none') +
    labs(subtitle = '(A) Income vs. NWR') +
    coord_cartesian(ylim = c(0, .4)),
  nw6 +
    theme(legend.position = 'none') +
    labs(subtitle = '(B) Net Worth vs. NWR') +
    coord_cartesian(ylim = c(0, .4)),
  ncol = 1
)

pl = plot_grid(
  tmp,
  get_legend_35(
    nw6 + theme(legend.position = 'bottom') +
      guides(color = guide_legend(nrow = 1, title = '        Campaign'))
    ),
  ncol = 1,
  rel_heights = c(1, .05)
)

ggsave(filename = 'figures/figA10.pdf', plot = pl, width = 6.5, height = 6.5)
